package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Employee;
import DataStorageClasses.Job;
import Database.DatabaseConnector;

@WebServlet("/GetJobInfoServlet")
public class GetJobInfoServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		try {
			DatabaseConnector db = new DatabaseConnector();
			long jobID = Long.parseLong((String) request.getParameter("jobID"));
			
			Job job = db.getJob(jobID);
			ArrayList<Employee> employees = db.getEmployeesOnJob(jobID);
			
			JSONObject result = new JSONObject();
			JSONArray jArray = new JSONArray();
			
			for(Employee employee: employees) {
				jArray.put(employee.getJSONObject());
			}
			
			result.put("job", job.getJSONObject());
			result.put("employees", jArray);
			response.getWriter().print(result);
			response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
		} catch (IOException e) {
			e.printStackTrace();
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		} catch (JSONException e) {
			e.printStackTrace();
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
	}
}
